package com.sf.admin.controller.sys;

import com.sf.admin.model.po.User;
import com.sf.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("sys/user")
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping
	public String selectAll(User user, Model model){
		List<User> userList = userService.selectAll(user);
		model.addAttribute("userList",userList);
		return "admin/rbac/rbac-user-list";
	}

	@RequestMapping("/add")
	public String add(){
		return "admin/rbac/rbac-user-add";
	}

	@RequestMapping("/save")
	public String save(User user){
		int save = userService.save(user);
		return "redirect:/sys/user";
	}

	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable long id){
		User user = new User();
		user.setUserId(id);
		int del = userService.delete(user);
		return "redirect:/sys/user";
	}

	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable long id,Model model){
		User user = new User();
		user.setUserId(id);
		model.addAttribute("u",userService.getUserById(user));
		return "admin/rbac/rbac-user-edit";
	}

	@RequestMapping("/update")
	public String update(User user){
		int update = userService.update(user);
		return "redirect:/sys/user";
	}

}
